package hui.com.controller.ue;

import hui.com.model.entity.UserEntity;
import hui.com.model.req.users.RequestAddUserPerms;
import hui.com.model.req.users.RequestAddUserRole;
import hui.com.model.req.users.RequestGetUserRoleAndPerms;
import hui.com.model.res.MyResponse;
import hui.com.model.res.users.ResponseGetUserRoleAndPerms;
import hui.com.service.UserLogService;
import hui.com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

@CrossOrigin(origins = "*")//处理跨源资源共享
@RequestMapping("/public/users")
@Controller
public class ue {
    @Resource
    UserService userService;
    @Resource
    UserLogService userLogService;


    /**
     * @ 待完善
     * 新增用户
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public Object addUser(@RequestBody UserEntity user) {

        return MyResponse.builder().data(userService.addUser(user)).code(200).build();
    }

    /**
     * 修改用户
     */
    @RequestMapping("/updUser")
    @ResponseBody
    public Object updUser(@RequestBody UserEntity user) {

        return MyResponse.builder().data(userService.updUser(user)).code(200).build();
    }

    /**
     * 获取用户的身份和角色
     */

    @RequestMapping("/getUserRoleAndPerms")
    @ResponseBody
    public Object getUserRoleAndPerms(@RequestBody(required = false) RequestGetUserRoleAndPerms userRoleAndPerms) {
        if(userRoleAndPerms==null){
            userRoleAndPerms = new RequestGetUserRoleAndPerms();
        }

        ResponseGetUserRoleAndPerms res =new ResponseGetUserRoleAndPerms();
        res.setUsers(userService.getUserRoleAndPerms(userRoleAndPerms));
        res.setPageMax(userService.getUserRoleAndPermsPage(userRoleAndPerms));
        return MyResponse.builder().data(res).code(200).build();
    }

    /**
     * 指定用户，赋予角色
     */
    @RequestMapping("/addUserRole")
    @ResponseBody
    public Object addUserRole(@RequestBody RequestAddUserRole requestAddUserRole) {
        if(userService.addUserRole(requestAddUserRole.getUID(), requestAddUserRole.getRoleIDArray())){
            userLogService.updUIDUserPermission(requestAddUserRole.getUID());
            return MyResponse.builder().data(true).code(200).build();
        }else{
            return MyResponse.builder().data(false).code(202).build();
        }

    }

    @RequestMapping("/deleteUserRole")
    @ResponseBody
    public Object deleteUserRole(@RequestBody  Map<String, Integer> map) {
        Integer UID = map.get("UID");
        Integer roleID = map.get("roleID");
        if(UID==null||roleID==null){
            return MyResponse.builder().msg("失败传参有误"+map).code(202).build();
        }
        if(userService.deleteUserRole(UID, roleID)){
            userLogService.updUIDUserPermission(UID);
            return MyResponse.builder().data(true).code(200).build();
        }else{
            return MyResponse.builder().data(false).code(200).build();
        }
    }

    @RequestMapping("/addUserPerms")
    @ResponseBody
    public Object addUserPerms(@RequestBody RequestAddUserPerms userPerms) {
        if(userPerms.getUID()==null || userPerms.getPermsIDArray()==null){
            return MyResponse.builder().data(false).code(202).build();
        }
        if(userService.addUserPerms(userPerms.getUID(),userPerms.getPermsIDArray())){
            userLogService.updUIDUserPermission(userPerms.getUID());
            return MyResponse.builder().data(true).code(200).build();
        }else{
            return MyResponse.builder().data(false).code(202).build();
        }
    }

    @RequestMapping("/deleteUserPerms")
    @ResponseBody
    public Object deleteUserPerms(@RequestBody Map<String,Integer> map) {
        Integer UID = map.get("UID");
        Integer permsID = map.get("permsID");
        if(UID==null||permsID==null){
            return MyResponse.builder().msg("失败,传参有误"+map).code(202).build();
        }

        if(userService.deleteUserPerms(UID,permsID)){
            userLogService.updUIDUserPermission(UID);
            return MyResponse.builder().data(true).code(200).build();
        }else{
            return MyResponse.builder().data(false).code(200).build();
        }
    }

    @RequestMapping("/getNotINUserPerms")
    @ResponseBody
    public Object getNotINUserPerms(@RequestBody UserEntity user) {


        return MyResponse.builder().data(userService.getNotINUserPerms(user)).code(200).build();
    }

    @RequestMapping("/getNotINUserRole")
    @ResponseBody
    public Object getNotINUserRole(@RequestBody UserEntity user) {

        return MyResponse.builder().data(userService.getNotINUserRole(user)).code(200).build();
    }
}
